﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using DBControl;
using System.Data;
using Top.Api.Request;
using Top.Api.Response;
using Top.Api;

namespace SellerProject
{
    /// <summary>
    /// SessionKey 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class SessionKey : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }



        [WebMethod]
        public void getSessionKey()
        {
            SqlDbHelper sdh = new SqlDbHelper();
            DataTable dt = sdh.ExecuteDataTable("SELECT * FROM UserSession");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DateTime dt1 = DateTime.Parse(dt.Rows[i]["UpdateTime"].ToString());

                if (DateTime.Now.Subtract(dt1).Minutes > 1)
                {
                    ITopClient itop = new DefaultTopClient("http://gw.api.tbsandbox.com/router/rest", "12167888", "sandbox3606eac16f68e582b5b5e52ba");
                    ItemsOnsaleGetRequest itemsRequest = new ItemsOnsaleGetRequest();
                    itemsRequest.Fields = "title";
                    itemsRequest.PageSize = 2;
                    itemsRequest.PageNo = 1;
                    ItemsOnsaleGetResponse itemsReponse = itop.Execute(itemsRequest, dt.Rows[i]["SessionKey"].ToString());
                    string updatestr = "UPDATE UserSession SET UpdateTime='"+ DateTime.Now +"' WHERE UserName='"+ dt.Rows[i]["UserName"] +"'";
                    sdh.ExecuteNonQuery(updatestr);
                }
            }
        }


        //[WebMethod]
        //public void addItem()
        //{
        //    string appKey = "12167888";
        //    string appSecret = "sandbox3606eac16f68e582b5b5e52ba";
        //    ITopClient itop = new DefaultTopClient("http://gw.api.tbsandbox.com/router/rest", appKey, appSecret);
        //    ItemGetRequest itemget = new ItemGetRequest();
        //    itemget.NumIid = 6000190936;
        //    itemget.Nick = "kico841201";
        //    itemget.Fields = "title,nick,type,skus,props_name,promoted_service,cid,props,input_pids,input_str,desc,pic_url,stuff_status,location,price,post_fee,express_fee,ems_fee,freight_payer,approve_status";
        //    //itemget.Fields = "title,nick,approve_status";
        //    ItemGetResponse itemresponse = itop.Execute(itemget);

        //    ItemAddRequest itemadd = new ItemAddRequest();
        //    itemadd.Num = 1;
        //    itemadd.Price = itemresponse.Item.Price;
        //    itemadd.Type = itemresponse.Item.Type;
        //    itemadd.StuffStatus = itemresponse.Item.StuffStatus;
        //    itemadd.Cid = itemresponse.Item.Cid;
        //    itemadd.Title = itemresponse.Item.Title + "123" + DateTime.Now.ToShortTimeString();
        //    itemadd.Desc = itemresponse.Item.Desc;
        //    itemadd.LocationState = itemresponse.Item.Location.State;
        //    itemadd.LocationCity = itemresponse.Item.Location.City;
        //    itemadd.Props = itemresponse.Item.Props;

        //    //itemadd.ListTime = DateTime.Parse("2011-03-28 12:32:00");
        //    itemadd.IsTaobao = true;
        //    //itemadd.IsEx = true;
        //    ItemAddResponse itemaddResponse = itop.Execute(itemadd, "29840f66da5f8322f8668dc95c9c77b3b545a");
            
        //}
    }
}
